﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;
using System.IO;

public partial class admin_xiugai : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
                BindContent();
        }
    }

    protected void BindContent()
    {
        string str = "select * from tb_picture where pic_ID=@pic_ID";
        int id = int.Parse(Request.QueryString["pic_ID"].ToString().Trim());
        OleDbParameter[] param = { 
                                     new OleDbParameter("@pic_ID",id)
                                 };
        OleDbDataReader dr = OleDbHelper.GetDataReader(str, param);
        while (dr.Read())
        {
            txt_title.Text = dr["pic_Title"].ToString();

            Pic.ImageUrl = dr["pic_address"].ToString();
         
        }
        dr.Close();
        dr.Dispose();
        btn_sure.Text = "修改";
    }

    //清除
    protected void btn_clear_Click(object sender, EventArgs e)
    {
        ClearTxt();
    }

    //清除函数
    protected void ClearTxt()
    {
        txt_title.Text = "";
   
        Pic.ImageUrl = "";
        btn_sure.Text = "添加";
     
    }

    protected void btn_upload_Click(object sender, EventArgs e)
    {
        if (fld_img.HasFile)
        {
            string path = Server.MapPath("~/upload/");
            string filestr = Path.GetExtension(fld_img.FileName).ToLower();
            string[] allowtype = { ".jpg", ".jpeg", ".png", "gif", ".bmp" };
            bool tf = false;
            for (int i = 0; i < allowtype.Length; i++)
            {
                if (filestr == allowtype[i])
                {
                    tf = true;
                }
            }
            if (tf)
            {
                try
                {
                    fld_img.PostedFile.SaveAs(path + fld_img.FileName);
                    Page.ClientScript.RegisterStartupScript(GetType(), "提示", "<script>alert('上传成功！')</script>");
                    Pic.ImageUrl = "~/upload/" + fld_img.FileName;
                   
                }
                catch (Exception)
                {

                    Page.ClientScript.RegisterStartupScript(GetType(), "提示", "<script>alert('上传失败！')</script>");
                }

            }
            else
            {
                Page.ClientScript.RegisterStartupScript(GetType(), "提示", "<script>alert('不支持给文件格式！')</script>");
            }
        }
        else
        {
            Page.ClientScript.RegisterStartupScript(GetType(), "提示", "<script>alert('请选择文件！')</script>");
        }
    }

    //添加  修改
    protected void btn_sure_Click(object sender, EventArgs e)
    {
        string update = "update tb_picture set pic_Title=@pic_Title,pic_address=@pic_address where pic_ID=@pic_ID";
        OleDbParameter[] param = { 
                                     new OleDbParameter("@pic_Title",txt_title.Text.ToString().Trim()),
                                     new OleDbParameter("@pic_address",Pic.ImageUrl.ToString()),
                                     new OleDbParameter("@pic_ID",int.Parse(Request.QueryString["pic_ID"].ToString()))
                                 };
        int flag = OleDbHelper.ExecuteNonSql(update, param);
        if (flag > 0)
        {
            Page.ClientScript.RegisterStartupScript(GetType(), "提示", "<script>alert('修改成功！')</script>");
            ClearTxt();
        }
        else {
            Page.ClientScript.RegisterStartupScript(GetType(), "提示", "<script>alert('修改失败，请稍候再试！')</script>");
        }
    }
}